package com.cynteka.cyfoman.core;

import com.cynteka.cyfoman.core.IPropertyResolver;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * User: Sergey
 * Date: 03.08.11
 * Time: 17:30
 */
public class MessagePropertyResolver implements IPropertyResolver {
    public static final String ID_PN = "id";
    public static final String FROM_PN = "from";
    public static final String SUBJECT_PN = "subject";
    public static final String SENT_DATE_PN = "sentDate";
    public static final String RECEIVED_DATE_PN = "receivedDate";
    public static final String CONTENT_PN = "content";


    @Override
    public Object getProperty(Object o, String propertyName) {
        Message message = null;
        try {
            if (o instanceof MimeMessage) {
                MimeMessage mimeMessage = (MimeMessage) o;
                if(ID_PN.equals(propertyName)) {
                    return mimeMessage.getMessageID();
                }

            }
            if (o instanceof Message)
                message = (Message) o;
            if (message == null)
                return null;
            if (SUBJECT_PN.equals(propertyName)) {
                return message.getSubject();
            } else if (FROM_PN.equals(propertyName)) {
                String from = InternetAddress.toString(message.getFrom());
                String ret = MimeUtility.decodeText(from);
                return ret;
            } else if (SENT_DATE_PN.equals(propertyName)) {
                return message.getSentDate();
            } else if (RECEIVED_DATE_PN.equals(propertyName)) {
                return message.getReceivedDate();
            } else if (CONTENT_PN.equals(propertyName)) {
                Object content = message.getContent();
                String sContent = String.valueOf(content);

                if(content instanceof Multipart) {
                    Multipart multipart = (Multipart) content;
                    for(int i = 0; i < multipart.getCount(); i++) {
                        BodyPart bodyPart = multipart.getBodyPart(i);
                        sContent = sContent + bodyPart;
                        if(bodyPart instanceof MimeBodyPart) {
                        }
                    }

                }


                return sContent;
            }
        } catch (javax.mail.MessagingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }
}
